package com.tistory.wonsama.test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;

public class CygwinExec {
	public static void main(String[] args) throws Exception {
        Process p;
        InputStream in;
        BufferedReader br;
        String line;
 
        // using single command String
        {
            String cmd;
            cmd = "C:/cygwin/bin/bash -c '/bin/ls -la'";
            System.out.println("EXECING: " + cmd);
            p = Runtime.getRuntime().exec(cmd);
 
            in = p.getInputStream();
            br = new BufferedReader(new InputStreamReader(in));
            System.out.println("OUT:");
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
 
            in = p.getErrorStream();
            br = new BufferedReader(new InputStreamReader(in));
            System.out.println("ERR:");
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
 
            System.out.println();
        }
 
        // using command array
        {
            String[] cmdArray;
            cmdArray = new String[] {"C:/cygwin/bin/bash", "-c", "/bin/ls -la"};
            System.out.println("EXECING: " + Arrays.toString(cmdArray));
            p = Runtime.getRuntime().exec(cmdArray);
 
            in = p.getInputStream();
            br = new BufferedReader(new InputStreamReader(in));
            System.out.println("OUT:");
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
 
            in = p.getErrorStream();
            br = new BufferedReader(new InputStreamReader(in));
            System.out.println("ERR:");
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
 
            System.out.println();
        }
    }
}
